
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.SensorPort;
import lejos.nxt.Sound;
/**
 * A simple line follower for the LEGO 9797 car with
 * a light sensor. Before the car is started on a line
 * a BlackWhiteSensor is calibrated to adapt to different
 * light conditions and colors.
 * 
 * The light sensor should be connected to port 3. The
 * left motor should be connected to port C and the right 
 * motor to port B.
 * 
 * @author  Ole Caprani
 * @version 23.08.07
 */
public class LineFolColSen
{
  public static void main (String[] aArg)
  throws Exception
  {
     final int power = 80;
	  
     ColorSensor sensor = new ColorSensor(SensorPort.S1);
     GoalSensor gSensor = new GoalSensor(SensorPort.S2);
	 
     gSensor.calibrate();
     sensor.calibrate();
     
     LCD.clear();
     LCD.drawString("Light: ", 0, 2); 
	 
     while (! Button.ESCAPE.isPressed())
     {

	     LCD.drawInt(sensor.light(),4,10,2);
	     LCD.refresh();
	     
	     switch ( sensor.getColor() ){
	     case BLACK:
	    	 Car.forward(power, 0);
	    	 break;
	     case WHITE:
	    	 Car.forward(0, power);
	    	 break;
	     case BLUE:
	    	 
	    	 if(gSensor.isGoal()){
	    		 Sound.twoBeeps();
	    		 Car.stop();
	    		 return;
	    	 }
	    	 else{
	    		 Car.forward(power, power);
	    	 }
	    	 break;
	     }
	     
	     Thread.sleep(10);
     }
     
     Car.stop();
     LCD.clear();
     LCD.drawString("Program stopped", 0, 0);
     LCD.refresh();
   }
}
